package com.lp.service;

import com.lp.domain.Girl;
import com.lp.enums.ResultEnum;
import com.lp.exception.GirlException;
import com.lp.repository.GirlRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by lp
 * 2017/11/7 13:23
 */
@Service
public class GirlService {
    @Autowired
    private GirlRepository girlRepository;

    @Transactional
    public void insertTwo(){
        Girl girlA = new Girl();
        girlA.setCupSize("EE");
        girlA.setAge(21);
        girlRepository.save(girlA);

        Girl girlB = new Girl();
        girlB.setCupSize("FFF");
        girlB.setAge(18);
        girlRepository.save(girlB);
    }

    public void getAge(Integer id) throws Exception {
        Girl girl = girlRepository.findOne(id);
        Integer age=girl.getAge();
        if (age<10){
           throw  new GirlException(ResultEnum.PRIMARY_SCHOOL);
        }else if(age>10 && age <16){
            throw  new GirlException(ResultEnum.MIDDLE_SCHOOL);
        }else{
            throw  new Exception();
        }
    }

    /**
     * 根据id查找女生
     * @param id
     * @return
     */
    public Girl findOne(Integer id){
        return girlRepository.findOne(id);
    }
}
